﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Service;
using System.Configuration;

namespace Web.SysManage.Product
{
    public partial class ProductEdit : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ServiceFactory.CreateInstance<CategoriesService>().BindCategory(selCategory, Part.RWDC, 0);
                selCategory.Items.Insert(0, new ListItem("请选择", "0"));
                //this.RoleInit();
                if (!string.IsNullOrEmpty(CommonUtils.Get("ProductID")))
                {
                    Title = "修改书籍";
                    hProductID.Value = CommonUtils.Get("ProductID");
                    ProductInfo product = ServiceFactory.CreateInstance<ProductService>().GetModel(CommonUtils.ToInt(hProductID.Value));
                    this.DataBind(product);
                }
                else
                {
                    Title = "添加书籍";
                }
            }
            //else
            //{
            //    this.AjaxMethod();
            //}
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void DataBind(IModelBase model)
        {
            ProductInfo product = model as ProductInfo;
            iTitle.Value = product.Title;
            iAuthor.Value = product.Author;
            iPublishHourse.Value = product.PublishHourse;
            iPrice.Value = product.Price.ToString();
            if (product.IsInstock == YesNo.Yes) { this.rInstockY.Checked = true; } else { this.rInstockN.Checked = true; }
            if (product.IsPublish == YesNo.Yes) { this.cIsPublishY.Checked = true; }
            taContent.Value = product.Content;
        }

        private ProductInfo GetModel(ref ProductInfo model)
        {
            string title = CommonUtils.Post("iTitle");
            string author = CommonUtils.Post("iAuthor");
            string publishhourse = CommonUtils.Post("iPublishHourse");
            string price = CommonUtils.Post("iPrice");
            YesNo isInStock = (YesNo)CommonUtils.ToInt(CommonUtils.Post("rInstock"));
            YesNo isPublish = (YesNo)CommonUtils.ToInt(CommonUtils.Post("cIsPublishY"));
            string smallimg = string.Empty;
            string bigimg = string.Empty;
            HttpFileCollection files = Request.Files;
            if (files.Count > 0)
            {
                HttpPostedFile file = files[0];
                string normalPath = ConfigurationManager.AppSettings["ImgNormal"];   //普通图片路径
                string smallPath = ConfigurationManager.AppSettings["ImgSmall"];        //缩略图片路径
                FileUploadHelper fileUpload = new FileUploadHelper();
                fileUpload.PostedFile = file;
                fileUpload.SetFile(); //设置文件信息
                //保存缩略图到small中
                fileUpload.FileCompressPath = smallPath;
                fileUpload.CompressImage(218, 203, "Cut");
                //保存图片到normal中
                fileUpload.FilePath = normalPath;
                fileUpload.SaveFile();
                smallimg = fileUpload.RefSmallFilePath;
                bigimg = fileUpload.RefSmallFilePath;
            }
            string content = CommonUtils.Post("taContent");

            if (string.IsNullOrEmpty(title))
            {
                //base.AjaxErch("{state:false,message:'用户名和昵称不能为空!'}");
                base.ResponseErch("书籍名不能为空!");
                return null;
            }

            model.Title = title;
            model.Author = author;
            model.PublishHourse = publishhourse;
            model.Price = CommonUtils.ToDecimal(price);
            model.IsInstock = isInStock;
            model.IsPublish = isPublish;
            model.BigImg = bigimg;
            model.SmallImg = smallimg;
            model.Content = content;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                ProductInfo product = string.IsNullOrEmpty(hProductID.Value) ? new ProductInfo() : ServiceFactory.CreateInstance<ProductService>().GetModel(CommonUtils.ToInt(hProductID.Value));
                product = GetModel(ref product);

                if (!string.IsNullOrEmpty(hProductID.Value))
                {
                    rtmessage = ServiceFactory.CreateInstance<ProductService>().UpdateProduct(base.adminInfo.AdminName, product);
                    resultMsg = "书籍信息修改成功!";
                }
                else
                {
                    rtmessage = ServiceFactory.CreateInstance<ProductService>().AddProduct(base.adminInfo.AdminName, product);
                    resultMsg = "成功添加新的书籍!";
                }
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}